Previous Book Contents Book Index Next

Inside Macintosh: AppleScript Language Guide / Part 2 - AppleScript Language Reference
Chapter 6 - Expressions / Operations


Operator Precedence

AppleScript allows you to combine expressions into larger, more complex expressions. When evaluating expressions, AppleScript uses operator precedence to determine which operations are performed first. Table 6-2 shows the order in which AppleScript performs operations.

To see how operator precedence works, consider the following expression.

2 * 5 + 12
--result: 22
To evaluate the expression, AppleScript performs the multiplication operation 2 * 5 first, because as shown in Table 6-2, multiplication has higher precedence than addition.

The column labeled "Associativity" in Table 6-2 indicates the order in which AppleScript performs operations if there are two or more operations of the same precedence in an expression. The word "none" in the Associativity column indicates that you cannot have multiple consecutive occurrences of the operation in an expression. For example, the expression 3 = 3 = 3 is not legal because the associativity for the equal operator (=) is "none." The word "unary" indicates that the operator is a unary operator. To evaluate expressions with multiple unary operators of the same order, AppleScript applies the operator closest to the operand first, then applies the next closest operator, and so on. For example, the expression not not not true is evaluated as not (not (not true)).
Operator precedence
OrderOperatorsAssociativityType of operator
1( )Innermost to outermostGrouping
2+
-
UnaryPlus or minus sign for numbers
3^Right to leftExponentiation
4*
/
÷
div
mod
Left to rightMultiplication and division
5+
-
Left to rightAddition and subtraction
6asLeft to rightCoercion
7<

>
NoneComparison
8=
NoneEquality and inequality
9notUnaryLogical negation
10andLeft to rightLogical for Boolean values
11orLeft to rightLogical for Boolean values

You can change the order in which AppleScript performs operations by grouping expressions in parentheses. As shown in Table 6-2, AppleScript evaluates expressions in parentheses first. For example, adding parentheses around 5 + 12 in the following expression causes AppleScript to perform the addition operation first and changes the result.

2 * ( 5 + 12 )
--result:34 

Previous Book Contents Book Index Next

© Apple Computer, Inc.
13 JUL 1996